package com.vn.ld.common.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Shark{loi.do.duy}
 */
public class PageObjInfo implements Serializable, Cloneable {

	private static final long serialVersionUID = 4550128306204041285L;

	public final static int MAX_OFFSET = 10000;
	public final static int DEFAULT_SQL_INDEX = 0;
	public final static int DEFAULT_INDEX = 1;
	public final static int DEAULT_OFFSET = 100;

	public int offset = DEAULT_OFFSET;
	public long total = 0;

	public int maxtotalDisplayPage = 100;
	public int indexPage = DEFAULT_INDEX;

	public PageObjInfo() {
	}

	public PageObjInfo(int offset, int indexPage) {
		this.offset = offset;
		this.indexPage = indexPage;
	}

	/**
	 * @return current index
	 */
	public int getIndex() {
		return (indexPage - 1) * offset;
	}

	/**
	 * @return last record
	 */
	public int getLastRecord() {
		return (indexPage * offset);
	}

	/**
	 * @return offset
	 */
	public int getOffset() {
		return offset;
	}

	/**
	 * Set offset
	 * 
	 * @param offset
	 */
	public void setOffset(int offset) {
		if (offset > 0) {
			this.offset = offset;
		}
	}

	/**
	 * @return Total page
	 */
	public long total() {
		return total;
	}

	/**
	 * @param total
	 */
	public void setTotal(long total) {
		if (total > 0)
			this.total = total;
	}

	/**
	 * @return indexPage
	 */
	public int getIndexPage() {
		return indexPage;
	}

	/**
	 * Set indexPage
	 * 
	 * @param intexPage
	 */
	public void setIndexPage(int indexPage) {
		if (indexPage > 0)
			this.indexPage = indexPage;
	}

	/**
	 * @return totalPage
	 */
	public long getTotalPage() {
		if (offset == 0) {
			return 0;
		} else {
			long totalPage = total / offset;
			long divPage = total % offset;
			return (divPage != 0 ? totalPage + 1 : totalPage);
		}
	}

	public List<PageItemInfo> createListPageItem(boolean descOrder) {
		List<PageItemInfo> listPages = new ArrayList<PageItemInfo>();

		long totalPage = this.getTotalPage();
		this.indexPage = this.getIndex();

		if (totalPage > (long) this.maxtotalDisplayPage) {
			int hIndex = indexPage + maxtotalDisplayPage / 2;
			hIndex = (hIndex < totalPage ? hIndex : (int) totalPage);
			if (hIndex <= maxtotalDisplayPage) {
				hIndex = maxtotalDisplayPage;
			}

			int lIndex = hIndex - maxtotalDisplayPage + 1;
			lIndex = (lIndex < 1 ? 1 : lIndex);

			if (descOrder) {
				for (int i = hIndex; i >= lIndex; i--) {
					listPages.add(new PageItemInfo(i, String.valueOf(i), (i == indexPage)));
				}
			} else {
				for (int i = lIndex; i <= hIndex; i++) {
					listPages.add(new PageItemInfo(i, String.valueOf(i), (i == indexPage)));
				}
			}
		} else {
			if (descOrder) {
				for (int i = (int) totalPage; i > 0; i--) {
					listPages.add(new PageItemInfo(i, String.valueOf(i), (i == indexPage)));
				}
			} else {
				for (int i = 1; i < (int) totalPage; i++) {
					listPages.add(new PageItemInfo(i, String.valueOf(i), (i == indexPage)));
				}
			}
		}

		return listPages;
	}

	public String toString() {
		return "PagingInfo-[index:" + getIndex() + ", offset:" + offset + ", total:" + total + ", indexPage:" + indexPage + ", totalPage:" + getTotalPage()
				+ "]";
	}
}
